package com.itcat.api;/*
 * @Author: gonhsl
 * @Date:2022-04-04-10:03
 * */

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itcat.api.mysql.SmsFlashPromotionSessionApi;
import com.itcat.mapper.SmsFlashPromotionSessionMapper;
import com.itcat.mysql.pojo.SmsFlashPromotionSession;
import com.itcat.mysql.vo.SmsFlashPromotionSessionDetail;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionSessionApiImpl implements SmsFlashPromotionSessionApi {
    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    @Override
    public List<SmsFlashPromotionSession> list() {
        List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectList(null);

        return smsFlashPromotionSessions;
    }

    @Override
    public List<SmsFlashPromotionSessionDetail> selectList(Integer flashPromotionId) {
        LambdaUpdateWrapper<SmsFlashPromotionSession> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SmsFlashPromotionSession::getStatus,1);
        List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectList(wrapper);

        List<SmsFlashPromotionSessionDetail> detailList = smsFlashPromotionSessions.stream().map(item -> {
            SmsFlashPromotionSessionDetail smsFlashPromotionSessionDetail = new SmsFlashPromotionSessionDetail();
            Integer integer = smsFlashPromotionSessionMapper.selectbyFlashPromotionId(flashPromotionId,item.getId());
            BeanUtils.copyProperties(item, smsFlashPromotionSessionDetail);
            smsFlashPromotionSessionDetail.setProductCount(integer);
            return smsFlashPromotionSessionDetail;
        }).collect(Collectors.toList());
        return detailList;
    }

    @Override
    public void create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.insert(smsFlashPromotionSession);
    }

    @Override
    public void delete(Integer id) {
        smsFlashPromotionSessionMapper.deleteById(id);
    }

    @Override
    public void updateByStatus(Integer id, Integer status) {
        LambdaUpdateWrapper<SmsFlashPromotionSession> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SmsFlashPromotionSession::getId,id).set(SmsFlashPromotionSession::getStatus,status);
        SmsFlashPromotionSession smsFlashPromotionSession = new SmsFlashPromotionSession();
        smsFlashPromotionSession.setStatus(status);
        smsFlashPromotionSessionMapper.update(smsFlashPromotionSession,wrapper);
    }

    @Override
    public void update(Integer id, SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.deleteById(smsFlashPromotionSession);
    }

    @Override
    public SmsFlashPromotionSession findOne(Integer id) {
        SmsFlashPromotionSession smsFlashPromotionSession = smsFlashPromotionSessionMapper.selectById(id);
        return smsFlashPromotionSession;
    }
}
